/**
*	This script tags the class sortable-tree with tree classes and makes it.
*	
*	Depends on:
*		jquery-1.9.1.js
*/
function TreeTagger(providedTree, startCollapsed, enableCollapseButton, listType) {
	/**
	 * Adds class 'collapsed' to all elements of class 'branch' in this.tree if startCollapsed == true
	 * and hides the sub list.
	 * If startCollapsed == false, elements of class 'branch' in list gets classed 'expanded'.
	 */
	var collapseTree = function (collapse) {
		tree.find('.'+self.BRANCH_TAG).each(function() {
			if(collapse) {
				$(this).addClass(self.COLLAPSED_TAG);
				$(this).removeClass(self.EXPANDED_TAG);
				$(this).children(self.LIST_TYPE).hide();
				$(this).children().first().find('.'+self.BUTTON_TAG).text('+');
				
			} else {
				$(this).addClass(self.EXPANDED_TAG);
				$(this).removeClass(self.COLLAPSED_TAG);
				$(this).children().first().find('.'+self.BUTTON_TAG).text('-');
			}
		});
	};

	/**
 	 *	Tags the items of the tree with rootTag, leafTag, and branchTag.
 	 *	Adds a collapse-button if none and enabled.
 	 */
	this.refreshTags = function() {
		var items = tree.find(self.LIST_ITEM);
		items.each(function() {
			var item = $(this);
			
			//.root tagging
			if(item.parent().is(tree)) {
				item.addClass(self.ROOT_TAG);
			} else {
				item.removeClass(self.ROOT_TAG);
			}
			
			//.branch and .leaf tagging
			var collapseButton = item.children().first().find('.'+self.BUTTON_TAG);
			if(item.children(self.LIST_TYPE).children(self.LIST_ITEM).length == 0) {
				item.removeClass(self.BRANCH_TAG);
				item.addClass(self.LEAF_TAG);
				item.children(self.LIST_TYPE).remove();
				
				//remove collapse-button if enabled and it exists
				if(!self.ENABLE_COLLAPSE || collapseButton.length > 0) {
					collapseButton.remove();
					item.removeClass(self.EXPANDED_TAG).removeClass(self.COLLAPSED_TAG);
				}
			} else {
				item.addClass(self.BRANCH_TAG);
				item.removeClass(self.LEAF_TAG);
				
				//add collapse button if enabled and there's none
				if(self.ENABLE_COLLAPSE && collapseButton.length == 0) {
					item.children().first().prepend($(self.BUTTON));
					 //No collapse-button => prev. empty => expanded tag
					item.addClass(self.EXPANDED_TAG);
				}
			}
		});
	};

	/**
	 *	Init
	 */
	var self = this;
	var tree = providedTree;
	this.START_COLLAPSED = startCollapsed;
	this.ENABLE_COLLAPSE = enableCollapseButton;
	this.LIST_TYPE = listType;

	//Apply tags
	this.refreshTags();

	if(this.ENABLE_COLLAPSE) {
		collapseTree(this.START_COLLAPSED);
	}

	/**
	 *	Listeners
	 */
	//Allow for (un-)collapsing through button-click.
	tree.on('click', '.'+self.BUTTON_TAG, function() {
		var parentItem = $(this).closest(self.LIST_ITEM);
		
		//Hide the sub-list
		var subList = parentItem.children().filter(self.LIST_TYPE);
		subList.slideToggle();
		
		//Mark collapsed/expanded
		parentItem.toggleClass(self.COLLAPSED_TAG).toggleClass(self.EXPANDED_TAG);
		var buttonText = parentItem.hasClass(self.EXPANDED_TAG) ? '-' : '+';
		$(this).text(buttonText);
	});
};

//Static properties
TreeTagger.prototype.LIST_TYPE = 'ol';
TreeTagger.prototype.LIST_ITEM = 'li';
TreeTagger.prototype.BRANCH_TAG = 'branch';
TreeTagger.prototype.ROOT_TAG = 'root';
TreeTagger.prototype.LEAF_TAG = 'leaf';

TreeTagger.prototype.ENABLE_COLLAPSE = true;
TreeTagger.prototype.START_COLLAPSED = true;
TreeTagger.prototype.COLLAPSED_TAG = 'collapsed';
TreeTagger.prototype.EXPANDED_TAG = 'expanded';
TreeTagger.prototype.BUTTON_TAG = 'collapse-button';
TreeTagger.prototype.BUTTON = '<span class="collapse-button">-</span>';